﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FacadeApi.Extension;
using FacadeApi.Types;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OtherLib.TenXun;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FileController : ControllerBase
    {
        public ILogger<FileController> _logger { get; set; }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="uploadFileType">0:头像  1:任务图片 2:聊天图片</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter, Authorize]
        public ActionResult UploadFiles(UploadFileType uploadFileType)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                List<string> list = new List<string>();
                foreach (var formFile in Request.Form.Files)
                {
                    byte[] bytes;
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        formFile.CopyTo(memoryStream);
                        bytes = memoryStream.ToArray();
                    }

                    var path = CosUtil.UploadObject($"app/user-{uid}/{uploadFileType.ToString()}/{Guid.NewGuid().ToString("N")}{Path.GetExtension(formFile.FileName)}", bytes);

                    list.Add(path);
                }

                return this.Success(list);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

        [AllowAnonymous, NotHamcSign]
        [HttpPost]
        public ActionResult UploadFilesByIM(string prefix)
        {
            try
            {
                List<string> list = new List<string>();
                foreach (var formFile in Request.Form.Files)
                {
                    byte[] bytes;
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        formFile.CopyTo(memoryStream);
                        bytes = memoryStream.ToArray();
                    }

                    var path = CosUtil.UploadObject($"im/{prefix}/{Guid.NewGuid().ToString("N")}{Path.GetExtension(formFile.FileName)}", bytes);

                    list.Add(path);
                }

                return this.Success(list);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
    }
}
